﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DeliveryService
{
    public class BLRestaurant
    {

        public bool RestaurantLogin(int restaurantId, string password)
        {
            using (DeliveryDBDataContext db = new DeliveryDBDataContext())
            {
                return db.Restaurants.Any(r => r.Restaurant_Id == restaurantId && r.Restaurant_Pwd == password);
            }
        }

        public bool ChangeStatus(int restaurantId, bool haveList)
        {
            using (DeliveryDBDataContext db = new DeliveryDBDataContext())
            {
                var rs = db.Restaurants.SingleOrDefault(r => r.Restaurant_Id == restaurantId);
                if (rs != null)
                {
                    rs.HaveList = haveList;
                    rs.RequestListTime = DateTime.Now;
                    db.SubmitChanges();
                    return true;
                }
                else throw new ArgumentException("can not found this restaurant.");
            }
        }

        public List<MRestaurant> GetRestaurants(int couriserId)
        {
            using (DeliveryDBDataContext db = new DeliveryDBDataContext())
            {
                var query = from c in db.Couriers
                            from r in c.Restaurants
                            where c.Courier_Id == couriserId && r.HaveList.HasValue && r.HaveList.Value == true
                            orderby r.RequestListTime descending
                            select new MRestaurant
                            {
                                RestaurantId = r.Restaurant_Id,
                                RestaurantName = r.Restaurant_Name,
                                RequestTime = r.RequestListTime,
                                IsBusy = r.IsBusy.HasValue ? (bool)r.IsBusy.Value : false
                            };
                return query.ToList();
            }
        }
        public List<MRestaurant> GetRestaurants(string couriserName)
        {
            using (DeliveryDBDataContext db = new DeliveryDBDataContext())
            {
                var query = from c in db.Couriers
                            from r in c.Restaurants
                            where c.Courier_Name == couriserName && r.HaveList.HasValue && r.HaveList.Value == true
                            orderby r.RequestListTime
                            select new MRestaurant
                            {
                                RestaurantId = r.Restaurant_Id,
                                RestaurantName = r.Restaurant_Name,
                                RequestTime = r.RequestListTime,
                                IsBusy = r.IsBusy.HasValue ? (bool)r.IsBusy.Value : false
                            };
                return query.ToList();
            }
        }

        public List<MRestaurant> GetRestaurants()
        {
            using (DeliveryDBDataContext db = new DeliveryDBDataContext())
            {
                var query = from r in db.Restaurants
                            where r.HaveList.HasValue && r.HaveList.Value == true
                            orderby r.RequestListTime
                            select new MRestaurant
                            {
                                RestaurantId = r.Restaurant_Id,
                                RestaurantName = r.Restaurant_Name,
                                RequestTime = r.RequestListTime,
                                IsBusy = r.IsBusy.HasValue ? (bool)r.IsBusy.Value : false
                            };
                return query.ToList();
            }
        }
        public List<MRestaurant> GetAllRestaurants()
        {
            using (DeliveryDBDataContext db = new DeliveryDBDataContext())
            {
                var query = db.Restaurants.OrderBy(s => s.Restaurant_Pwd).Select(s => new MRestaurant
                {
                    RestaurantId = s.Restaurant_Id,
                    RestaurantName = s.Restaurant_Name,
                    RequestTime = s.RequestListTime,
                    IsBusy = s.IsBusy.HasValue ? (bool)s.IsBusy.Value : false
                });
                return query.ToList();
            }
        }

        public string GetAllRestaurantList()
        {
            using (DeliveryDBDataContext db = new DeliveryDBDataContext())
            {
                var query = db.Restaurants.Select(s => new MRestaurant
                {
                    RestaurantId = s.Restaurant_Id,
                    RestaurantName = s.Restaurant_Name,
                    RequestTime = s.RequestListTime,
                    IsBusy = s.IsBusy.HasValue ? (bool)s.IsBusy.Value : false
                });
                return JsonConvert.SerializeObject(query);
            }
        }

        public List<MRestaurant> SerachRestaurantByName(string restaurantName)
        {
            using (DeliveryDBDataContext db = new DeliveryDBDataContext())
            {
                var query = db.Restaurants.Where(r => r.Restaurant_Name.Contains(restaurantName))
                                          .Select(r => new MRestaurant
                                            {
                                                RestaurantId = r.Restaurant_Id,
                                                RestaurantName = r.Restaurant_Name,
                                                RequestTime = r.RequestListTime,
                                                IsBusy = r.IsBusy.HasValue ? (bool)r.IsBusy.Value : false
                                            });
                return query.ToList();
            }
        }

        /// <summary>
        /// 修改餐馆的忙碌状态
        /// </summary>
        /// <param name="restaurantId">餐馆ID</param>
        /// <param name="isBusy">是否忙碌</param>
        /// <returns>s是否修改成功</returns>
        public bool ChangeRestaurantIsBusy(int restaurantId, bool isBusy)
        {
            using (DeliveryDBDataContext db = new DeliveryDBDataContext())
            {
                var rs = db.Restaurants.SingleOrDefault(r => r.Restaurant_Id == restaurantId);
                if (rs != null)
                {
                    rs.IsBusy = isBusy;
                    db.SubmitChanges();
                    return true;
                }
                // else throw new ArgumentException("can not found this restaurant.");
                else
                    return false;
            }
        }
    }
}